package com.borqs.samples;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class DataActivity extends Activity implements OnClickListener,
        OnItemClickListener {
    private final String TAG = "DataActivity";

    private Button mAlarmButton, mWakelockButton, mProcessButton;
    private ListView mList = null;

    private Cursor mCursor = null;

    private int mSamplesId = 0;
    private View alamHead, wakelockHead, processHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_layout);
        mSamplesId = getIntent().getExtras().getInt("1");
        findView();
    }

    private void findView() {
        mAlarmButton = (Button) findViewById(R.id.alarm_button);
        mWakelockButton = (Button) findViewById(R.id.wakelock_button);
        mProcessButton = (Button) findViewById(R.id.process_button);
        mList = (ListView) findViewById(R.id.data_list);

        mAlarmButton.setOnClickListener(this);
        mWakelockButton.setOnClickListener(this);
        mProcessButton.setOnClickListener(this);

        alamHead = findViewById(R.id.alarm_head);
        wakelockHead = findViewById(R.id.wakelock_head);
        processHead = findViewById(R.id.process_head);

        setHeadVisibility(View.GONE);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

    }

    private void setHeadVisibility(int visibility) {
        alamHead.setVisibility(visibility);
        wakelockHead.setVisibility(visibility);
        processHead.setVisibility(visibility);
    }

    @Override
    public void onClick(View v) {
        setHeadVisibility(View.GONE);

        String[] from = null;
        int[] to = null;
        switch (v.getId()) {
        case R.id.alarm_button:
            alamHead.setVisibility(View.VISIBLE);
            mCursor = query(DBProvider.AlarmSamplesTable.CONTENT_URI,
                    DBProvider.AlarmSamplesTable.SAMPLE_ID, mSamplesId);

            from = new String[] { DBProvider.AlarmSamplesTable.SAMPLE_ID,
                    DBProvider.AlarmSamplesTable.PKG,
                    DBProvider.AlarmSamplesTable.COUNT };
            to = new int[] { R.id.start_time, R.id.start_average_v,
                    R.id.start_charge };

            if (null != mCursor) {
                setListAdapter(R.layout.sessions_list_item, mCursor, from, to);
            }
            break;
        case R.id.wakelock_button:
            wakelockHead.setVisibility(View.VISIBLE);
            mCursor = query(DBProvider.WakeLockSamplesTable.CONTENT_URI,
                    DBProvider.WakeLockSamplesTable.SAMPLE_ID, mSamplesId);

            from = new String[] { DBProvider.WakeLockSamplesTable.SAMPLE_ID,
                    DBProvider.WakeLockSamplesTable.TIME_LENGTH,
                    DBProvider.WakeLockSamplesTable.TAG };
            to = new int[] { R.id.start_time, R.id.start_average_v,
                    R.id.start_charge };

            if (null != mCursor) {
                setListAdapter(R.layout.sessions_list_item, mCursor, from, to);
            }

            break;
        case R.id.process_button:
            processHead.setVisibility(View.VISIBLE);
            mCursor = query(DBProvider.ProcessSamplesTable.CONTENT_URI,
                    DBProvider.ProcessSamplesTable.SAMPLE_ID, mSamplesId);

            from = new String[] { DBProvider.ProcessSamplesTable.SAMPLE_ID,
                    DBProvider.ProcessSamplesTable.PID,
                    DBProvider.ProcessSamplesTable.REL_STIME,
                    DBProvider.ProcessSamplesTable.REL_UPTIME,
                    DBProvider.ProcessSamplesTable.REL_UTIME };
            to = new int[] { R.id.start_time, R.id.start_average_v,
                    R.id.start_charge, R.id.stop_time, R.id.stop_average_v };

            if (null != mCursor) {
                setListAdapter(R.layout.process_list_item, mCursor, from, to);
            }
            break;
        default:
            break;
        }

    }

    private void setListAdapter(int layout, Cursor c, String[] from, int[] to) {
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
                layout, c, from, to, 0);
        mList.setAdapter(simpleCursorAdapter);
        c = null;
    }

    private Cursor query(Uri uri, String whereRow, int where) {
        Cursor c = null;

        if (null != whereRow) {
            c = getContentResolver().query(uri, null, whereRow + "=?",
                    new String[] { where + "" }, "_id ASC");
        } else {
            c = getContentResolver().query(uri, null, null, null, "_id ASC");
        }

        if (c != null && c.moveToFirst()) {
            return c;
        } else {
            Log.i(TAG, "query failure!");
        }
        return null;
    }
}
